﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.AfterSale
{
    public partial class ASale_QCChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txt_CUSNAM.OnClientTriggerClick = Window1.GetSaveStateReference(hid_CUSNAM.ClientID, txt_CUSNAM.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectCustom.aspx?r=1", "选择客户");
                LoadData();
                BindGrid();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                AfSale_QcInfo qcinfo = TbsDbSession.Default.From<AfSale_QcInfo>().Where(AfSale_QcInfo._.ID == int.Parse(Request["id"])).ToFirst();
                txt_ADDRES.Text = qcinfo.ADDRES;
                txt_CONTAT.Text = qcinfo.CONTAT;
                txt_CUINFO.Text = qcinfo.CUINFO;
                txt_CUSIGN.Text = qcinfo.CUSIGN;
                txt_CUSNAM.Text = qcinfo.CUSNAM;
                txt_ENGNAM.Text = qcinfo.ENGNAM;
                txt_FOXNUB.Text = qcinfo.FOXNUB;
                if (qcinfo.FRTIME.HasValue)
                    txt_FRTIME.Text = qcinfo.FRTIME.Value.ToString("yyyy-MM-dd");
                txt_PDBACH.Text = qcinfo.PDBACH;
                if (qcinfo.PUTIME.HasValue)
                    txt_PUTIME.Text = qcinfo.PUTIME.Value.ToString("yyyy-MM-dd");
                txt_SRINFO.Text = qcinfo.SRINFO;
                txt_SUGTON.Text = qcinfo.SUGTON;
                txt_TELNUB.Text = qcinfo.TELNUB;
                if (qcinfo.TOTIME.HasValue)
                    txt_TOTIME.Text = qcinfo.TOTIME.Value.ToString("yyyy-MM-dd");
                txt_UEMAIL.Text = qcinfo.UEMAIL;
                txt_USAGEC.Text = qcinfo.USAGEC;
                hf.Text = qcinfo.ATTACH;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            // PageContext.RegisterStartupScript("top.dynamic_added_tab_tab_ASale_QCList.location.href='http://www.baidu.com';");
            //Alert.Show("回访记录更新成功！", "提示", "top.dynamic_added_tab_tab_qcadd.location.href='http://www.baidu.com';RemTab('dynamic_added_tab_tab_qcadd');");
            #region 提交
            string mess = "";
            bool t = true;
            if (txt_CUSNAM.Text == "")
            {
                mess = "请选择客户";
                t = false;
                txt_CUSNAM.MarkInvalid(mess);
            }
            if (txt_ADDRES.Text == "")
            {
                mess = "请输入地址";
                t = false;
                txt_ADDRES.MarkInvalid(mess);
            }

            if (txt_CONTAT.Text == "")
            {
                mess = "请输入联系人";
                t = false;
                txt_CONTAT.MarkInvalid(mess);
            }


            if (txt_TELNUB.Text == "")
            {
                mess = "请输入电话";
                t = false;
                txt_TELNUB.MarkInvalid(mess);
            }


            if (txt_ENGNAM.Text == "")
            {
                mess = "请输入回访人员";
                t = false;
                txt_ENGNAM.MarkInvalid(mess);
            }


            if (txt_FRTIME.Text == "")
            {
                mess = "请输入到达时间";
                t = false;
                txt_FRTIME.MarkInvalid(mess);
            }



            if (txt_TOTIME.Text == "")
            {
                mess = "请输入离开时间";
                t = false;
                txt_TOTIME.MarkInvalid(mess);
            }



            if (txt_CUINFO.Text == "")
            {
                mess = "请输入客户情况简介";
                t = false;
                txt_CUINFO.MarkInvalid(mess);
            }

            if (txt_USAGEC.Text == "")
            {
                mess = "请输入产品使用情况";
                t = false;
                txt_USAGEC.MarkInvalid(mess);
            }


            if (txt_PUTIME.Text == "")
            {
                mess = "请输入购买时间";
                t = false;
                txt_PUTIME.MarkInvalid(mess);
            }


            if (txt_SRINFO.Text == "")
            {
                mess = "请输入服务质量";
                t = false;
                txt_SRINFO.MarkInvalid(mess);
            }


            if (txt_SUGTON.Text == "")
            {
                mess = "请输入客户意见与建议";
                t = false;
                txt_SUGTON.MarkInvalid(mess);
            }


            if (txt_CUSIGN.Text == "")
            {
                mess = "请输入客户签字";
                t = false;
                txt_CUSIGN.MarkInvalid(mess);
            }


            if (t)
            {
                try
                {
                    int returnvalue = 0;
                    AfSale_QcInfo qcInfo = new AfSale_QcInfo();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        qcInfo.ID = int.Parse(Request["id"]);
                        qcInfo.Attach();
                        qcInfo.Adder = CookInfo.EAdminTrueName();
                        qcInfo.ADDRES = txt_ADDRES.Text;
                        qcInfo.CONTAT = txt_CONTAT.Text;
                        qcInfo.CUINFO = txt_CUINFO.Text;
                        qcInfo.CUSIGN = txt_CUSIGN.Text;
                        qcInfo.CUSNAM = txt_CUSNAM.Text;
                        qcInfo.CUTNUB = hid_CUSNAM.Text;
                        qcInfo.ENGNAM = txt_ENGNAM.Text;
                        qcInfo.FOXNUB = txt_FOXNUB.Text;
                        qcInfo.FRTIME = Convert.ToDateTime(txt_FRTIME.Text);
                        qcInfo.PDBACH = txt_PDBACH.Text;
                        qcInfo.PUTIME = Convert.ToDateTime(txt_PUTIME.Text);
                        qcInfo.SRINFO = txt_SRINFO.Text;
                        qcInfo.SUGTON = txt_SUGTON.Text;
                        qcInfo.TELNUB = txt_TELNUB.Text;
                        qcInfo.TOTIME = Convert.ToDateTime(txt_TOTIME.Text);
                        qcInfo.UEMAIL = txt_UEMAIL.Text;
                        qcInfo.USAGEC = txt_USAGEC.Text;
                        qcInfo.ATTACH = hf.Text;
                        returnvalue = TbsDbSession.Default.Update<AfSale_QcInfo>(qcInfo);
                    }
                    else
                    {
                        qcInfo.DeAttach();
                        qcInfo.Adder = CookInfo.EAdminTrueName();
                        qcInfo.ADDRES = txt_ADDRES.Text;
                        qcInfo.AddTime = DateTime.Now;
                        qcInfo.CONTAT = txt_CONTAT.Text;
                        qcInfo.CUINFO = txt_CUINFO.Text;
                        qcInfo.CUSIGN = txt_CUSIGN.Text;
                        qcInfo.CUSNAM = txt_CUSNAM.Text;
                        qcInfo.CUTNUB = hid_CUSNAM.Text;
                        qcInfo.ENGNAM = txt_ENGNAM.Text;
                        qcInfo.FOXNUB = txt_FOXNUB.Text;
                        qcInfo.FRTIME = Convert.ToDateTime(txt_FRTIME.Text);
                        qcInfo.PDBACH = txt_PDBACH.Text;
                        qcInfo.PUTIME = Convert.ToDateTime(txt_PUTIME.Text);
                        qcInfo.QCDATE = DateTime.Now;
                        qcInfo.SRINFO = txt_SRINFO.Text;
                        qcInfo.SUGTON = txt_SUGTON.Text;
                        qcInfo.TELNUB = txt_TELNUB.Text;
                        qcInfo.TOTIME = Convert.ToDateTime(txt_TOTIME.Text);
                        qcInfo.UEMAIL = txt_UEMAIL.Text;
                        qcInfo.USAGEC = txt_USAGEC.Text;
                        qcInfo.QCNUBR = DFieldSN.SN(EnumData.DataSNEunm.回访记录流水号);
                        qcInfo.ATTACH = hf.Text;
                        returnvalue = TbsDbSession.Default.Insert<AfSale_QcInfo>(qcInfo);
                    }
                    if (returnvalue > 0)
                    {
                        //PageContext.RegisterStartupScript("top.dynamic_added_tab_tab_Articles_List.main.location.reload();");
                        Alert.Show("回访记录更新成功！", "提示", "top.dynamic_added_tab_tab_ASale_QCList.location.reload();RemTab('dynamic_added_tab_tab_qcadd');");
                    }
                }
                catch (Exception ex)
                {

                    Alert.Show(ex.ToString());
                }
                
            }
            else
            {
                Alert.ShowInTop(mess);
            }
            #endregion
        }




        #region 文件上传
        List<TempFile> files = new List<TempFile>();
        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.HasFile)
            {
                string fileName = filePhoto.ShortFileName;
                String[] str = { "1", "2", "3", "4", "5", "6", "7" };
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                filePhoto.SaveAs(Server.MapPath("~/upload/" + fileName));
                hf.Text += fileName + ";";
                // 清空文件上传组件
                filePhoto.Reset();
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            List<string> ls = StringHelper.GetSubStringList(hf.Text, ';');
            foreach (string item in ls)
            {
                TempFile tf = new TempFile();
                tf.FileName = item;
                files.Add(tf);
            }
            Grid1.DataSource = files;
            Grid1.DataBind();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string[] str = Grid1.Rows[e.RowIndex].Values;
                hf.Text = hf.Text.Replace(str[1], "");
                BindGrid();
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[0] = "<a href=\"/upload/" + e.Values[0] + "\">" + e.Values[0] + "</a>";
        }
        #endregion


    }
}